package com.example.hello;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

/**
 * @maker twc
 * @ time 2021/4/19 13:57
 */
public class PopupWindowActivity extends AppCompatActivity implements View.OnClickListener {

    private Button       show;
    private Button       hide;
    private PopupWindow  pop;
    private LinearLayout ll;
    private Button notice;
    private Button call;
    private EditText mpho;
    private Button customview;
    final private int RED = 110;
    final private int GREEN = 111;
    final private int BLUE = 112;
    final private int YELLOW = 113;
    final private int GRAY= 114;
    final private int CYAN= 115;
    final private int BLACK= 116;
    private TextView tv_test;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop);
        init();
        startService(new Intent(this,CallService.class));
    }
/*
    菜单*/
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(1,RED,4,"红色");
        menu.add(1,GREEN,2,"绿色");
        menu.add(1,BLUE,3,"蓝色");
        menu.add(1,YELLOW,1,"黄色");
        menu.add(1,GRAY,5,"灰色");
        menu.add(1,CYAN,6,"蓝绿色");
        menu.add(1,BLACK,7,"黑色");
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id){
            case RED:
                tv_test.setTextColor(Color.RED);
                break;
            case GREEN:
                tv_test.setTextColor(Color.GREEN);
                break;
            case BLUE:
                tv_test.setTextColor(Color.BLUE);
                break;
            case YELLOW:
                tv_test.setTextColor(Color.YELLOW);
                break;
            case GRAY:
                tv_test.setTextColor(Color.GRAY);
                break;
            case CYAN:
                tv_test.setTextColor(Color.CYAN);
                break;
            case BLACK:
                tv_test.setTextColor(Color.BLACK);
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    private void init() {
        customview = findViewById(R.id.customview);
        customview.setOnClickListener(this);
//        call = findViewById(R.id.bt_call);
//        call.setOnClickListener(this);
//        mpho = findViewById(R.id.mpho);
        show = findViewById(R.id.show);
        show.setOnClickListener(this);
        hide = findViewById(R.id.hide);
        hide.setOnClickListener(this);
        ll = findViewById(R.id.ll);
        notice = findViewById(R.id.notification);
        notice.setOnClickListener(this);
        tv_test = findViewById(R.id.tv_test);
    }

    @Override
    public void onClick(View v) {
     switch (v.getId()){
         case  R.id.show:
             showPoP();
             break;
         case R.id.hide:
             pop.dismiss();
             break;
         case R.id.notification:
             openocd();
             break;
         case R.id.customview:
             Intent intent = new Intent(PopupWindowActivity.this,CustomView.class);
             startActivity(intent);
//         case R.id.bt_call:
//             String phone = mpho.getText().toString();
//             Intent intent = new Intent();
//             intent.setAction("android.intent.action.CALL");
//             intent.addCategory("android.intent.category.DEFAULT");
//             intent.setData(Uri.parse("tel:"+phone));
//             startActivity(intent);
//             break;

     }
    }

    private void openocd() {
        Notification.Builder builder = new Notification.Builder(PopupWindowActivity.this);
        //设置小图标
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //设置可返回
        builder.setAutoCancel(true);
        //设置大图标
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
        builder.setLargeIcon(bitmap);
        //设置标题
        builder.setContentTitle("斗罗大陆");
        //设置内容
        builder.setContentText("唐三复活了小舞");
        //设置说明
        builder.setSubText("新更新");


        //构造pendingIntent
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.xp7000.com/12/12817/"));
        PendingIntent pendingIntent = PendingIntent.getActivities(PopupWindowActivity.this, 0, new Intent[]{intent}, 0);
        //跳转
        builder.setContentIntent(pendingIntent);

        //发布
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel("AppTestNotificationId", "AppTestNotificationName", NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(notificationChannel);
            builder.setChannelId("AppTestNotificationId");
        }
        manager.notify(0,builder.build());
    }

    private void showPoP() {
        View view = getLayoutInflater().inflate(R.layout.item_laout,null);
        pop = new PopupWindow(view,LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT,true);
        pop.showAtLocation(ll, Gravity.CENTER,0,0);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (pop != null && pop.isShowing()){
                    pop.dismiss();
                }
                return false;
            }
        });
        view.findViewById(R.id.to).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(PopupWindowActivity.this,"点击了我",Toast.LENGTH_SHORT).show();
            }
        });
    }
}
